import routerPic from "../assets/images/router.svg";
import switchPic from "../assets/images/switch.svg";

const posList = ["-300 -300", "0 -150", "300 -300", "250 500"];

export const getNodes = (rawNode, rawSwitch) => {
  if (!rawNode) return [];
  const nodes = rawNode.map((r, i) => {
    const node = {
      key: r.name,
      type: "ROUTER",
      text: r.name,
      portLists: r.port,
      command: r.command,
      loc: posList[i],
      // pic: routerPic
    };
    return node;
  });

  if (rawSwitch) {
    nodes.push({
      key: rawSwitch.name,
      type: "SWITCH",
      text: rawSwitch.name,
      // pic: routerPic
    });
  }

  nodes.forEach((n) => {
    n.pic = n.type === "ROUTER" ? routerPic : switchPic;
  });
  return nodes;
};

export const getLinks = (rawLink, rawNode) => {
  if (!rawLink) return [];
  const links = rawLink
    .filter((l) => l.logicalConnected)
    .map((l) => {
      const link = {
        from: l.from[0],
        to: l.to[0],
        fromPort: l.from[1],
        toPort: l.to[1],
        key: l.from[0] + "-" + l.to[0],
      };
      return link;
    });
  rawNode.forEach((n) => {
    n.port.forEach((p) => {
      if (p.name.startsWith("lo")) {
        links.push({
          from: n.name,
          to: n.name,
          loPort: p.name,
          key: p.name,
          // toPort:,
        });
      }
    });
  });
  return links;
};
